package com.help.help.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.*;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.excel.EasyExcel;
import com.help.help.util.ExceUtil;
import com.sun.deploy.net.URLEncoder;
import org.bouncycastle.util.encoders.UrlBase64Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.help.help.entity.AddressEntity;
import com.help.help.service.AddressService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author 小🌤
 * @email 1578442339@qq.com
 * @date 2021-01-07 15:41:11
 */
@RestController
@RequestMapping("/help/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = addressService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		AddressEntity address = addressService.getById(id);
        return R.ok().put("address", address);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AddressEntity address){
		addressService.save(address);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AddressEntity address){
		addressService.updateById(address);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		addressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 导出
     * @param response
     * @param ids
     * @throws IOException
     */
    @RequestMapping("/updaloa")
      public void   upload(HttpServletResponse response, Integer[] ids) throws IOException {
        List<AddressEntity> list = addressService.listByIds(Arrays.asList(ids));
         ExceUtil.out("地址信息导出", response);
        EasyExcel.write(response.getOutputStream(), AddressEntity.class).sheet("地址信息导出信息").doWrite(list);
      }
}
